package com.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.config.exceptionHandler.exception.GuliException;
import com.edu.entity.Course;
import com.edu.entity.CourseDescription;
import com.edu.entity.course.CourseWebVo;
import com.edu.entity.vo.CoursePublishVo;
import com.edu.entity.vo.CourseQuery;
import com.edu.mapper.CourseDescriptionMapper;
import com.edu.mapper.CourseMapper;
import com.edu.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author mxs
 * @since 2021-10-21
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Autowired
    private CourseDescriptionMapper descriptionMapper;

    /**
     * 保存课程信息 和 课程描述
     *
     * @param query
     * @return
     */
    @Override
    public String saveCourseInfo(CourseQuery query) {
        //向课程中添加信息
        Course course = new Course();
        BeanUtils.copyProperties(query, course);
        int insert = baseMapper.insert(course);
        if (insert == 0) {
            throw new GuliException(20001, "添加课程信息失败");
        }
        System.out.println(course.toString());
        //获取新增课程信息中的id,id在插入的时候自动创建了
        String cid = course.getId();
        CourseDescription courseDescription = new CourseDescription();

        BeanUtils.copyProperties(query, courseDescription);
        //保存课程信息
        courseDescription.setId(cid);
        descriptionMapper.insert(courseDescription);
        //返回课程id
        return course.getId();
    }

    /**
     * 获取课程信息
     *
     * @param courseId
     * @return
     */
    @Override
    public CourseQuery getCourseInfoByCourseId(Long courseId) {
        //获取课程信息
        Course course = baseMapper.selectById(courseId);
        //获取课程描述信息
        CourseDescription description = descriptionMapper.selectById(courseId);
        CourseQuery courseInfo = new CourseQuery();
        if (course == null){
            return null;
        }
        //将课程信息封装
        BeanUtils.copyProperties(course, courseInfo);
        //将描述信息封账
        courseInfo.setDescription(description.getDescription());
        return courseInfo;
    }

    /**
     * 修改课程信息
     *
     * @param courseInfo 课程信息的封装类
     */
    @Override
    public void updateCourseInfo(CourseQuery courseInfo) {
        if (courseInfo != null) {
            //先判断课程信息是否为空
            Course course = new Course();
            //将课程中的信息传递给课程
            BeanUtils.copyProperties(courseInfo, course);
            //更新课程
            int update = baseMapper.updateById(course);
            if (update == 0) {
                throw new GuliException(20001, "更新失败");
            }
            CourseDescription description = new CourseDescription();
            description.setDescription(courseInfo.getDescription());
            //更新课程描述
            descriptionMapper.updateById(description);
        }
    }

    @Override
    public CoursePublishVo getCoursePublish(String courseId) {
        return baseMapper.getPublishCourseInfo(courseId);
    }

    /**
     * 根据course获取条件类
     * @param course
     * @return
     */
    @Override
    public QueryWrapper<Course> getCourseQueryWrapper(Course course) {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        String title = course.getTitle();
        String status = course.getStatus();
        if (!StringUtils.isEmpty(title)){
            queryWrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(status)){
            queryWrapper.eq("status",status);
        }
        return queryWrapper;
    }

    /**
     * id降序 获取前8条数据
     * 用了redis作为缓存
     * @return
     */
    @Cacheable(value = "course",key = "'selectCourseList'")
    @Override
    public List<Course> getHotCourse() {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit 8");
        List<Course> courseList = baseMapper.selectList(queryWrapper);
        System.out.println("进入数据库查询课程数据");
        return courseList;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        CourseWebVo courseWebVo = baseMapper.getCourseInfo(courseId);
        return courseWebVo;
    }
}
